package wetalk.software.bupt.com.wetalk.communicate;


import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.jyt.message.Message;
import com.jyt.message.MessageListener;
import com.jyt.message.MessageServerTcpClient;
import com.jyt.util.ArgumentString;
import com.jyt.util.MyDate;
import com.jyt.util.MySerializable;

import wetalk.software.bupt.com.wetalk.util.WeTalkConfig;

import static android.content.ContentValues.TAG;

/**
 * Created by hp on 2018/1/12.
 */

public class AndroidClient extends MessageServerTcpClient {
    private Context context;

    public AndroidClient(String server_ip, String server_name, String entity,Context context) {
        super(server_ip, server_name, entity);
        this.context = context;
        MessageServerTcpClient manager = new MessageServerTcpClient(server_ip, server_name);
        manager.register(entity);
        addListener("msg", new ResponseListener());
        addListener("addFriRes",new AddFriResResponseListener());
        addListener("addFri",new AddFriResponseListener());
        addListener("pullFri",new PullFriResponseListener());
        addListener("pullUser",new PullFriResponseListener());
        addListener("pullGroup",new PullGroupResponseListener());
        addListener("fetchUserInfo", new FetchUserInfoResponseListener());
        addListener("loginRes", new LoginResponseListener());
        addListener("modifyUserInfo", new ModifyResponseListener());
        addListener("createGroupRes", new CreateGroupResponseListener());
        addListener("pullMemberRes", new PullMemberResponseListener());
        addListener("delMemberRes", new DelMemberResponseListener());
        addListener("quitGroupRes", new QuitGroupResponseListener());
        addListener("modifyGroupNameRes", new ModifyGroupNmaeResponseListener());
        addListener("msg_pic", new ResponseListener());
        addListener("msg_file", new ResponseListener());
        addListener("msg_voice", new ResponseListener());
        addListener("msg_lfile", new ResponseListener());
        addListener("delFriRes",new delFriResponseListener());
    }

    public AndroidClient(String server_ip, String server_name, String[] entity_list,Context context) {
        super(server_ip, server_name, entity_list);
        this.context=context;
    }
    public class delFriResponseListener implements MessageListener{
        @Override
        public void messagePerformed(Message message) {
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String from = message.getFrom();
            String time_str = MyDate.f2(message.getCreated());
            String content = (String)MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str,from,content};
            String result = ArgumentString.replace(field,ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction("delFriRes");
            intent.putExtra("type",type);
            intent.putExtra("content",content);
            context.sendOrderedBroadcast(intent,null);
        }
    }

    public class PullFriResponseListener implements MessageListener{
        public void messagePerformed(Message message){
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String from = message.getFrom();
            String time_str = MyDate.f2(message.getCreated());
            String content = (String)MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str,from,content};
            String result = ArgumentString.replace(field,ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction("pullFri");
            intent.putExtra("type",type);
            intent.putExtra("content",content);
            context.sendBroadcast(intent);
        }

    }

    public class AddFriResResponseListener implements MessageListener{
        public void messagePerformed(Message message){
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String from = message.getFrom();
            String time_str = MyDate.f2(message.getCreated());
            String content = (String)MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str,from,content};
            String result = ArgumentString.replace(field,ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction("addFriRes");
            intent.putExtra("content",content);
            context.sendOrderedBroadcast(intent,null);
        }

    }

    public class AddFriResponseListener implements MessageListener{
        public void messagePerformed(Message message){
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String from = message.getFrom();
            String time_str = MyDate.f2(message.getCreated());
            String content = (String)MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str,from,content};
            String result = ArgumentString.replace(field,ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction("addFri");
            intent.putExtra("content",content);
            context.sendBroadcast(intent);
        }

    }

    public class FetchUserInfoResponseListener implements MessageListener {
        public void messagePerformed(Message message)
        {
            String field = "**************\n%1 from %2 \n%3\n**************";
            String time_str = MyDate.f2(message.getCreated());
            String from = message.getFrom();
            String content = (String) MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str,from,content};
            String result = ArgumentString.replace(field,ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction("return_fetch_userInfo");
            intent.putExtra("fetchUserInfo",content);
            context.sendOrderedBroadcast(intent,null);
        }
    }

    public class ResponseListener implements MessageListener {
        public void messagePerformed(Message message)
        {
            String field = "**************\n%1 from %2 \n%3\n**************";
            String time_str = MyDate.f2(message.getCreated());
            String from = message.getFrom();
            String content = (String) MySerializable.bytes_object(message.getContent());
            String msgType=message.getType();
            String[] ss = new String[]{time_str,from,content};
            String result = ArgumentString.replace(field,ss);

            Log.i(TAG, "get message: "+result);

            Intent intent=new Intent();
            intent.setAction(WeTalkConfig.BROADCAST_NEW_MESSAGE);
            intent.putExtra("msg",content);
            intent.putExtra("msgFrom",from);
            intent.putExtra("msgType",msgType);
            intent.putExtra("msgFrom",from);
            context.sendOrderedBroadcast(intent,null);
        }
    }

    public class LoginResponseListener implements MessageListener {
        public void messagePerformed(Message message)
        {

            System.out.println("登录信息接收到");
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String from = message.getFrom();
            String time_str = MyDate.f2(message.getCreated());
            String content = (String)MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str,from,content};
            String result = ArgumentString.replace(field,ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction(WeTalkConfig.BROADCAST_USER_LOGIN);
            intent.putExtra("result",content);
            context.sendBroadcast(intent);
        }
    }

    public class FetchUserResponseListener implements MessageListener {
        public void messagePerformed(Message message)
        {

            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String from = message.getFrom();
            String time_str = MyDate.f2(message.getCreated());
            String content = (String)MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str,from,content};
            String result = ArgumentString.replace(field,ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction(WeTalkConfig.BROADCAST_USER_FETCH);
            intent.putExtra("fetchUserInfo",content);
            context.sendBroadcast(intent);
        }
    }

    public class ModifyResponseListener implements MessageListener {
        public void messagePerformed(Message message)
        {

            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String from = message.getFrom();
            String time_str = MyDate.f2(message.getCreated());
            String content = (String)MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str,from,content};
            String result = ArgumentString.replace(field,ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction(WeTalkConfig.BROADCAST_MODIFY_USER_INFO);
            intent.putExtra("result",content);
            context.sendOrderedBroadcast(intent,null);
        }
    }

    /**
     * 群聊部分
     */

    public class CreateGroupResponseListener implements MessageListener {
        public void messagePerformed(Message message) {
            //这里构造了json串，不需要这样，因为发送过来的文字本身就是结构完好的
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String time_str = MyDate.f2(message.getCreated());
            String from = message.getFrom();
            String content = (String) MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str, from, content};
            String result = ArgumentString.replace(field, ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction(WeTalkConfig.BROADCAST_CREATE_GROUP);
            intent.putExtra("result",content);
            context.sendBroadcast(intent,null);
        }
    }

    public class PullMemberResponseListener implements MessageListener {
        public void messagePerformed(Message message) {
            //这里构造了json串，不需要这样，因为发送过来的文字本身就是结构完好的
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String time_str = MyDate.f2(message.getCreated());
            String from = message.getFrom();
            String content = (String) MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str, from, content};
            String result = ArgumentString.replace(field, ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction(WeTalkConfig.BROADCAST_PULL_MEMBER_GROUP);
            intent.putExtra("result",content);
            context.sendBroadcast(intent,null);
        }
    }

    public class DelMemberResponseListener implements MessageListener {
        public void messagePerformed(Message message) {
            //这里构造了json串，不需要这样，因为发送过来的文字本身就是结构完好的
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String time_str = MyDate.f2(message.getCreated());
            String from = message.getFrom();
            String content = (String) MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str, from, content};
            String result = ArgumentString.replace(field, ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction(WeTalkConfig.BROADCAST_DELETE_MEMBER_GROUP);
            intent.putExtra("result",content);
            context.sendBroadcast(intent,null);
        }
    }

    public class QuitGroupResponseListener implements MessageListener {
        public void messagePerformed(Message message) {
            //这里构造了json串，不需要这样，因为发送过来的文字本身就是结构完好的
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String time_str = MyDate.f2(message.getCreated());
            String from = message.getFrom();
            String content = (String) MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str, from, content};
            String result = ArgumentString.replace(field, ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction(WeTalkConfig.BROADCAST_QUIT_GROUP);
            intent.putExtra("result",content);
            context.sendBroadcast(intent,null);
        }
    }

    public class ModifyGroupNmaeResponseListener implements MessageListener {
        public void messagePerformed(Message message) {
            //这里构造了json串，不需要这样，因为发送过来的文字本身就是结构完好的
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String time_str = MyDate.f2(message.getCreated());
            String from = message.getFrom();
            String content = (String) MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str, from, content};
            String result = ArgumentString.replace(field, ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction(WeTalkConfig.BROADCAST_CHANGE_GROUP_NAME);
            intent.putExtra("result",content);
            context.sendBroadcast(intent,null);
        }
    }
    public class PullGroupResponseListener implements MessageListener{
        public void messagePerformed(Message message){
            String field = "**************\n%1 from %2 \n%3\n**************";
            String type=message.getType();
            String from = message.getFrom();
            String time_str = MyDate.f2(message.getCreated());
            String content = (String)MySerializable.bytes_object(message.getContent());
            String[] ss = new String[]{time_str,from,content};
            String result = ArgumentString.replace(field,ss);

            Log.i(TAG, "get message: "+result);
            Intent intent=new Intent();
            intent.setAction(WeTalkConfig.BROADCAST_PULL_GROUP);
            intent.putExtra("content",content);
            context.sendBroadcast(intent);
        }

    }

//    public class GroupMsgResponseListener implements MessageListener {
//        public void messagePerformed(Message message) {
//            //这里构造了json串，不需要这样，因为发送过来的文字本身就是结构完好的
//            String field = "**************\n%1 from %2 \n%3\n**************";
//            String type=message.getType();
//            String time_str = MyDate.f2(message.getCreated());
//            String from = message.getFrom();
//            String content = (String) MySerializable.bytes_object(message.getContent());
//            String[] ss = new String[]{time_str, from, content};
//            String result = ArgumentString.replace(field, ss);
//
//            Log.i(TAG, "get message: "+result);
//            Intent intent=new Intent();
//            intent.setAction(WeTalkConfig.BROADCAST_CREATE_GROUP);
//            intent.putExtra("result",content);
//            context.sendBroadcast(intent,null);
//        }
//    }
}